// topic.js：话题管理API和工具函数
import request from '@/utils/request'

// 获取话题分页列表
export function getTopicList(params) {
    const payload = {
        page: params?.page || 0,
        pageSize: params?.pageSize || 0,
        category_id: params?.category_id || 0,
        keyWords: params?.keyWords || '',
        status: params?.status || '',
        visibility: params?.visibility || 0
    }

    // page、pageSize为正整数（从1开始）
    payload.page = Math.max(1, parseInt(payload.page) || 1)
    payload.pageSize = Math.max(1, parseInt(payload.pageSize) || 10)

    // category_id处理：如果为0或空，则移除该参数（表示查询所有分类）
    if (payload.category_id === '' || payload.category_id === null || payload.category_id === undefined || payload.category_id === 0) {
        delete payload.category_id
    } else {
        payload.category_id = parseInt(payload.category_id)
    }

    // visibility处理：如果为0或空，则移除该参数
    if (payload.visibility === '' || payload.visibility === null || payload.visibility === undefined || payload.visibility === 0) {
        delete payload.visibility
    } else {
        payload.visibility = parseInt(payload.visibility)
    }

    // status参数：如果为空字符串，则移除该参数（表示查询所有状态）
    if (payload.status === '' || payload.status === null || payload.status === undefined) {
        delete payload.status
    } else {
        payload.status = String(payload.status)
    }

    // keyWords参数：如果为空字符串，则移除该参数
    if (payload.keyWords === '' || payload.keyWords === null || payload.keyWords === undefined) {
        delete payload.keyWords
    }

    return request.post('/management/topic/pageTopicByManager', payload)
}

// 获取分类列表（复用文章的接口）
export function getCategoryList() {
    return request.get('/common/category')
}

// 动态分类映射（从API获取）
export function createCategoryMap(categoryList) {
    const map = {}
    if (Array.isArray(categoryList)) {
        categoryList.forEach(category => {
            map[category.categoryId] = category.name
        })
    }
    return map
}

// 状态名称映射（按后端约定，字符串状态）
export const statusMap = {
    '1': '草稿',
    '2': '已发布',
    '3': '隐藏',
    '4': '待审核',
    '5': '审核未通过',
    '6': '已封禁',
    '7': '已删除'
}

// 可见性名称映射
export const visibilityMap = {
    1: '公开',
    2: '匿名'
}

// 工具函数：获取状态标签类型
export function getStatusTagType(status) {
    const map = {
        '1': 'info',
        '2': 'success',
        '3': 'warning',
        '4': 'warning',
        '5': 'danger',
        '6': 'info',
        '7': 'danger'
    }
    const key = typeof status === 'number' ? String(status) : status
    return map[key] || 'info'
}

// 工具函数：获取可见性标签类型
export function getVisibilityTagType(visibility) {
    const map = {
        1: 'success',  // 公开 - 绿色
        2: 'warning'   // 匿名 - 橙色
    }
    return map[visibility] || 'info'
}

// 工具函数：获取分类标签类型（复用文章的）
export function getCategoryTagType(categoryId) {
    const typeMap = { jiuye: 'success', study: 'primary', emotion: 'warning' }
    return typeMap[categoryId] || 'info'
}

// 修改话题状态
export function updateTopicStatus({ id, reviewMessage, status }) {
    const payload = {
        id: String(id),
        reviewMessage: reviewMessage || '',
        status: typeof status === 'number' ? status : parseInt(status)
    }
    return request.post('/management/topic/updateStatus', payload)
}

// 获取话题详情
export function getTopicDetail(topicId) {
    return request.get('/management/topic/detail', {
        params: { topicId }
    })
}

// 提取话题精简内容
export function getTopicConciseContent(topicId) {
    return request.get('/management/topic/getConciseContent', {
        params: { topicId }
    })
}

// 批量删除话题
export function deleteTopics(topicIds = []) {
    // 接口要求body为字符串数组
    const ids = Array.isArray(topicIds) ? topicIds : [topicIds]
    return request.post('/management/topic/delete', ids)
}

